/**
 * Created by sxx on 2017/5/3.
 */
import "./index.less";
import { projSts } from "../config.js";

import bidListTpl from "./apply_list.handlebars";
import paginationTpl from "./paging.handlebars";

let token = sessionStorage.getItem('token');

var ApplyList = {
    pageNo: 1,
    pageSize: 20,
    maxPage: 1,
    isFirstPage: false,
    isLastPage: false,
    total: 0,
    init: function() {
        this.getData()
            .then(this.renderList)
            .then(this.renderPagination)
            .then(this.bindEvent);
    },
    getData: function() {
        return new Promise((resolve, reject) => {
            let self = this;
            $.ajax({
                url: HOST_ADDR + `/cpmp/splrMng/splrListByStatus/`,
                type: 'get',
                dataType: 'json',
                headers: {
                    'access_token': token,
                    'login_type': 'ums',
                    "splrStatus": "03,04,11,09,12,10",
                    "pageNo": this.pageNo,
                    "pageSize": this.pageSize
                },
                // data:JSON.stringify(data),
                contentType: "application/json",
                success: function(resp) {
                    if (resp.success) {
                        resolve(resp);
                    } else {
                        alert(resp.msgInf);
                    }
                },
                error: function(err) {
                    console.log(err);
                }
            });


        });

    },
    // 渲染列表

    renderList: function(resp) {
        let self = ApplyList;
        let config = {
            "03": "预备供方",
            "04": "开发申请",
            "09": "开发申请失败",
            "10": "主数据申请失败",
            "11": "BPM申请成功",
            "12": "主数据申请"
        };
        // 处理数据begin
        self.total = resp.data.count;
        self.isFirstPage = self.pageNo === 1;
        self.maxPage = Math.ceil(self.total / self.pageSize);
        $.each(resp.data.list,function(index,item){
            item.canApply = (item.splrSts === "03" || item.splrSts === "09" || item.splrSts === "10");
            item.canApplyMdm = (item.splrSts === "11");
            item.status = config[item.splrSts];
            item.adtMsg = item.adtMsg!=null ? item.adtMsg : '';
        });
        // 处理数据end
        if (self.pageNo * self.pageSize >= self.total) {
            self.isLastPage = true;
        } else {
            self.isLastPage = false;
        }

        let resultHtml = bidListTpl({ applyList: resp.data.list });
        $('.J_apply-list').html(resultHtml);
    },

    // 渲染分页
    renderPagination: function() {
        let self = ApplyList;
        let pageList = [];
        for (let i = 0; i < self.maxPage; i++) {
            pageList.push({ idx: i + 1 });
        }
        let paginationHtml = paginationTpl({
            begin: (self.pageNo - 1) * self.pageSize + 1,
            end: self.isLastPage ? self.total : self.pageNo * self.pageSize,
            total: self.total,
            isLastPage: self.isLastPage,
            isFirstPage: self.isFirstPage,
            pageList: pageList

        });
        $('.J_pagination').html(paginationHtml);
        $('.page-item').each((index, item) => {
            if ($(item).data('index') === self.pageNo) {
                $(item).addClass('active');
            }
            var basepage = self.pageNo;
            if(basepage<=5){
                basepage = 5;
            }
            if((-5 <= (parseInt($(item).data('index'))-basepage)) && ((parseInt($(item).data('index'))-basepage)<=5)){
                $(item).removeClass('hidden');
                $(item).find("span").removeClass("hidden");
            }else{
                $(item).addClass('hidden');
                $(item).find("span").addClass("hidden");
            }
        });
    },

    //主数据申请
    mdmApply: function(splrId) {
        console.log(token);
        console.log(splrId);
        $.ajax({
            url: HOST_ADDR + `/cpmp/cpmp/splrAply/${splrId}`,
            type: 'post',
            dataType: 'json',
            data: JSON.stringify({}),
            headers: {
                'access_token': token,
                'splrId': splrId,
                'login_type': 'ums'
            },
            contentType: "application/json",
            success: function(resp) {
                if (resp.success) {
                    alert('主数据申请成功');
                    ApplyList.init();
                } else {
                    alert(resp.msgInf);
                }
            },
            error: function(err) {
                console.log(err);
            }
        });
    },

    // 绑定事件
    bindEvent: function() {
        let self = ApplyList;
        $('.J_prev').unbind().click(function() {
            if (self.pageNo > 1) {
                self.pageNo = self.pageNo - 1;
                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }
        });
        $('.J_apply_mdm').unbind().click(function(e) {
            let splrId = $(e.target).data('id');
            console.log(splrId);
            console.log('apply');
            self.mdmApply(splrId);
        });
        $('.J_next').unbind().click(function() {
            console.log('J_next');

            if (self.pageNo < self.maxPage) {
                console.log('aaa');
                self.pageNo = self.pageNo + 1;
                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }

        });
        $('.page-item').off().on('click', function() {
            let $item = $(this);
            console.log('aaaa');
            if (!$item.hasClass('active')) {
                self.pageNo = $item.data('index');

                self.getData()
                    .then(self.renderList)
                    .then(self.renderPagination)
                    .then(self.bindEvent);
            }
        });
    },
};

App.init();
ApplyList.init();